#pragma once

class CExclusiveLock
{
public:
	CExclusiveLock(SRWLOCK& SRWLock)
		: m_SRWLock(SRWLock)
		, m_bLocked(false)
	{
		Lock();
	}

	~CExclusiveLock()
	{
		Unlock();
	}

	void Lock()
	{
		if (!m_bLocked)
		{
			m_bLocked = true;
			AcquireSRWLockExclusive(&m_SRWLock);
		}
	}

	void Unlock()
	{
		if (m_bLocked)
		{
			m_bLocked = false;
			ReleaseSRWLockExclusive(&m_SRWLock);
		}
	}

private:
	SRWLOCK& m_SRWLock;
	bool     m_bLocked;
};

class CSharedLock
{
public:
	CSharedLock(SRWLOCK& SRWLock)
		: m_SRWLock(SRWLock)
		, m_bLocked(false)
	{
		Lock();
	}

	~CSharedLock()
	{
		Unlock();
	}

	void Lock()
	{
		if (!m_bLocked)
		{
			m_bLocked = true;
			AcquireSRWLockShared(&m_SRWLock);
		}
	}

	void Unlock()
	{
		if (m_bLocked)
		{
			m_bLocked = false;
			ReleaseSRWLockShared(&m_SRWLock);
		}
	}

private:
	SRWLOCK& m_SRWLock;
	bool     m_bLocked;
};
